package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.UserBiz;
import com.mrd.gtimp.business.entity.CancelReason;
import com.mrd.gtimp.business.entity.Trail;
import com.mrd.gtimp.business.service.CancelReasonBiz;
import com.mrd.gtimp.business.service.TrailBiz;
import com.mrd.util.ModelPrinter;
import com.mrd.util.sendEmail.SendEmail;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */

@Controller
@RequestMapping("/business")
public class CancelReasonAction {
    private static final long serialVersionUID = 1L;

    @Resource
    private CancelReasonBiz cancelReasonBiz;
    @Resource
    private TrailBiz trailBiz;

    @Resource
    private UserBiz userBiz;

    /**
     * 添加取消理由
     */
    @RequestMapping("/cancelReason-cancel")
    public String cancel() {
        String id = HttpUtils.getParam("id");
        String type = HttpUtils.getParam("type");
        String businessId = HttpUtils.getParam("businessId");
//        request.setAttribute("id", id);
//        request.setAttribute("type", type);
//        request.setAttribute("businessId", businessId);
        HttpUtils.getRequest().setAttribute("id", id);
        HttpUtils.getRequest().setAttribute("type", type);
        HttpUtils.getRequest().setAttribute("businessId", businessId);
        return "/gtimp/cancelReason/cancel";
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/cancelReason-find")
    @ResponseBody
    public String find(CancelReason cancelReason) {
        if (null != cancelReason) {
            if (StringUtils.isNotEmpty(cancelReason.getId())) {
                try {
                    cancelReason = cancelReasonBiz.findEntityById(cancelReason.getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        ModelPrinter.print1(cancelReason);
        //// TODO: 2016/8/15   json
//        return "data";
        return null;
    }


    /**
     * 保存新增信息
     */
    @RequestMapping("/cancelReason-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(User user, CancelReason cancelReason, Trail trail) {
        Result msg;
        String id = HttpUtils.getParam("id");
        String type = HttpUtils.getParam("type");
        String businessId = HttpUtils.getParam("businessId");
        try {
            if (StringUtils.isEmpty(cancelReason.getId())) {
                cancelReason.setId(null);
                cancelReason.setCancelDate(new Date());
                cancelReason.setCancelPerson(user.getName());
                cancelReason.setBid(id);
            }
            cancelReasonBiz.saveOrupdateEntity(cancelReason);
            trail = trailBiz.findEntityById(id);
            if (type.equals("1")) {// 报关
                // declarationBiz.updateValidateReject(id);
            } else if (type.equals("2")) {// 拖车
                trailBiz.updateValidateApplyCancel(id);
            } else if (type.equals("3")) {// 订舱
                // bookingBiz.updateValidateReject(id);
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
            // 邮件,短信发送
            String user_email = "";
            String user_phone = "";
            String body = "业务编号为：" + businessId + "的拖车业务单申请取消，请尽快联系客户确认!";
            if ("佛山新港".equals(trail.getSupCompany().getName())) {
                List<User> userList = userBiz
                        .findEntityByHql("from com.mrd.gtimp.base.pojo.User where name in ('chenwc','niepy')", 0, 10, null);
                if (userList.size() > 0) {
                    for (User u : userList) {
                        // 修改页面提交后邮件发送
                        user_email = u.getEmail();
                        if (user_email != null) {
                            SendEmail.sendEmail(user_email, "货运平台下单通知", body);
                        }
                        // 客户进行取消订单申请提交后短信发送
                        // user_phone = u.getMobile();
                        // if(user_phone != null || !"".equals(user_phone)) {
                        // com.mrd.util.sendMessage.SendMessageAct
                        // .doSendPhoneMsg(user_phone, body);
                        // }
                    }
                }
            } else if ("宏达货代".equals(trail.getSupCompany().getName())) {
                List<User> userList = userBiz
                        .findEntityByHql("from com.mrd.gtimp.base.pojo.User where name in ('yaozd','zouyf','xiey','zoucl','zhoud')", 0, 10, null);
                if (userList.size() > 0) {
                    for (User u : userList) {
                        // 修改页面提交后邮件发送
                        user_email = u.getEmail();
                        if (user_email != null) {
                            SendEmail.sendEmail(user_email, "货运平台下单通知", body);
                        }
                        // 客户进行取消订单申请提交后短信发送
                        // user_phone = u.getMobile();
                        // if(user_phone != null || !"".equals(user_phone)) {
                        // com.mrd.util.sendMessage.SendMessageAct
                        // .doSendPhoneMsg(user_phone, body);
                        // }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    public CancelReasonBiz getCancelReasonBiz() {
        return cancelReasonBiz;
    }

    public void setCancelReasonBiz(CancelReasonBiz cancelReasonBiz) {
        this.cancelReasonBiz = cancelReasonBiz;
    }

    public TrailBiz getTrailBiz() {
        return trailBiz;
    }

    public void setTrailBiz(TrailBiz trailBiz) {
        this.trailBiz = trailBiz;
    }


    public UserBiz getUserBiz() {
        return userBiz;
    }

    public void setUserBiz(UserBiz userBiz) {
        this.userBiz = userBiz;
    }

}
